package org.squirrel.modules.bigscreen.controller;

import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.squirrel.common.api.vo.Result;
import org.squirrel.common.system.base.controller.SquirrelController;
import org.squirrel.modules.bigscreen.dto.DataSetDto;
import org.squirrel.modules.bigscreen.dto.OriginalDataDto;
import org.squirrel.modules.bigscreen.entity.DataSet;
import org.squirrel.modules.bigscreen.param.DataSetTestTransformParam;
import org.squirrel.modules.bigscreen.service.DataSetService;

import com.alibaba.fastjson.JSON;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * 数据集
 * 
 * @author leixiangjian
 *
 */
@Slf4j
@Api(tags = "数据集")
@RestController
@RequestMapping("/dataSet")
public class DataSetController extends SquirrelController<DataSet, DataSetService> {

	@Autowired
	private DataSetService dataSetService;

	/**
	 * 根据ID查询数据集详情
	 * 
	 * @param id
	 * @return
	 */
	@ApiOperation(value = "根据ID查询数据集详情", notes = "根据ID查询结果集详情")
	@GetMapping("/detailBysetId/{id}")
	public Result<?> detailBysetId(@PathVariable("id") Long id) {
		try {
			log.info("{}根据ID查询服务开始，id为：{}", this.getClass().getSimpleName(), id);
			DataSetDto dataSetDto = dataSetService.detailSet(id);
			log.info("{}根据ID查询结束，结果：{}", this.getClass().getSimpleName(), JSON.toJSONString(dataSetDto));
			return Result.OK("根据ID查询数据集成功", dataSetDto);
		} catch (Exception e) {
			log.error("根据ID查询数据集结果异常", e);
			return Result.error("根据ID查询数据集结果失败,原因:" + e.getMessage());
		}
	}

	/**
	 * 根据编码查询数据集详情
	 * 
	 * @param setCode
	 * @return
	 */
	@ApiOperation(value = "根据Code查询数据集详情", notes = "根据Code查询结果集详情")
	@GetMapping({ "/detailBysetCode/{setCode}" })
	public Result<?> detailBysetCode(@PathVariable("setCode") String setCode) {
		try {
			log.info("{}根据setCode查询服务开始，setCode为：{}", this.getClass().getSimpleName(), setCode);
			DataSetDto dataSetDto = dataSetService.detailSet(setCode);
			log.info("{}根据setCode查询结束，结果：{}", this.getClass().getSimpleName(), JSON.toJSONString(dataSetDto));
			return Result.OK("根据CODE查询数据集成功", dataSetDto);
		} catch (Exception e) {
			log.error("根据CODE查询数据集结果异常", e);
			return Result.error("根据CODE查询数据集结果失败,原因:" + e.getMessage());
		}
	}

	/**
	 * 新增数据集
	 * 
	 * @param dto
	 * @return
	 */
	@ApiOperation(value = "新增数据集", notes = "新增数据集")
	@PostMapping
	public Result<?> insert(@RequestBody DataSetDto dto) {
		try {
			log.info("{}新增服务开始，参数：{}", this.getClass().getSimpleName(), JSON.toJSONString(dto));
			DataSetDto dataSetDto = dataSetService.insertSet(dto);
			log.info("{}新增服务结束，结果：{}", this.getClass().getSimpleName(), JSON.toJSONString(dataSetDto));
			return Result.OK("根据CODE查询数据集成功", dataSetDto);
		} catch (Exception e) {
			log.error("新增数据集结果异常", e);
			return Result.error("新增数据集结果失败,原因:" + e.getMessage());
		}
	}

	/**
	 * 更新数据集
	 * 
	 * @param dto
	 * @return
	 */
	@ApiOperation(value = "更新数据集", notes = "更新数据集")
	@PutMapping
	public Result<?> update(@RequestBody DataSetDto dto) {
		try {
			log.info("{}更新服务开始,参数：{}", this.getClass().getSimpleName(), JSON.toJSONString(dto));
			dataSetService.updateSet(dto);
			log.info("{}更新服务结束，结果：{}", this.getClass().getSimpleName(), JSON.toJSONString(dto));
			return Result.OK("更新数据集成功", dto);
		} catch (Exception e) {
			log.error("更新数据集结果异常", e);
			return Result.error("更新数据集结果失败,原因:" + e.getMessage());
		}
	}

	/**
	 * 根据ID删除数据集
	 * 
	 * @param id
	 * @return
	 */
	@ApiOperation(value = "根据ID删除数据集", notes = "根据ID删除数据集")
	@DeleteMapping({ "/{id}" })
	public Result<?> deleteById(@PathVariable("id") Long id) {
		try {
			log.info("{}删除服务开始，参数ID：{}", this.getClass().getSimpleName(), id);
			dataSetService.deleteSet(id);
			log.info("{}删除服务结束", this.getClass().getSimpleName());
			return Result.OK("更新数据集成功", id);
		} catch (Exception e) {
			log.error("删除数据集结果异常", e);
			return Result.error("删除数据集结果失败,原因:" + e.getMessage());
		}
	}

	/**
	 * 测试数据转换是否正确
	 * 
	 * @param param
	 * @return
	 */
	@ApiOperation(value = "测试数据转换是否正确", notes = "测试数据转换是否正确")
	@PostMapping("/testTransform")
	public Result<?> testTransform(@Validated @RequestBody DataSetTestTransformParam param) {
		try {
			DataSetDto dto = new DataSetDto();
			BeanUtils.copyProperties(param, dto);
			OriginalDataDto originalDataDto = dataSetService.testTransform(dto);
			return Result.OK("测试数据转换是否正确成功", originalDataDto);
		} catch (Exception e) {
			log.error("测试数据转换是否正确异常", e);
			return Result.error("测试数据转换是否正确失败,原因:" + e.getMessage());
		}
	}

	/**
	 * 获取所有数据集
	 * 
	 * @return
	 */
	@ApiOperation(value = "获取所有数据集", notes = "获取所有数据集")
	@GetMapping("/queryAllDataSet")
	public Result<?> queryAllDataSet() {
		try {
			List<DataSet> dataSetLts = dataSetService.queryAllDataSet();
			return Result.OK("获取所有数据集成功", dataSetLts);
		} catch (Exception e) {
			log.error("测试数据转换是否正确异常", e);
			return Result.error("测试数据转换是否正确失败,原因:" + e.getMessage());
		}
	}

}
